package cn.yzx.pethome.system.menu.controller;

import cn.yzx.pethome.annotation.PreAuthorize;
import cn.yzx.pethome.system.dept.domain.Department;
import cn.yzx.pethome.system.dept.query.DepartmentQuery;
import cn.yzx.pethome.system.menu.service.IMenuService;
import cn.yzx.pethome.system.menu.domain.Menu;
import cn.yzx.pethome.system.menu.query.MenuQuery;
import cn.yzx.pethome.utils.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.httpclient.cookie.CookieAttributeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name ="保存菜单",value = "menu:save")
    public AjaxResult addOrUpdate(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null)
                menuService.update(menu);
            else
                menuService.add(menu);
            return AjaxResult.success().setMsg("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    /**
    * 删除菜单信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "根据id删除菜单")
    @PreAuthorize(name ="删除菜单",value = "menu:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            menuService.delete(id);
            return AjaxResult.success().setMsg("保存成功！");
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询菜单")
    @PreAuthorize(name ="查询一条菜单",value = "menu:findOne")
    public Menu get(@PathVariable("id")Long id)
    {
        return menuService.findOne(id);
    }


    /**
    * 查看所有的菜单信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name ="查询所有菜单",value = "menu:findAll")
    public List<Menu> list(){

        return menuService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询菜单
    * @return PageList 菜单分页
    */
    @PostMapping("/pagelist")
    @ApiOperation(value = "查询菜单")
    @PreAuthorize(name ="分页查询菜单",value = "menu:list")
    public AjaxResult json(@RequestBody MenuQuery query)
    {
        try {
            List<Menu> byQuery =  menuService.findByQuery(query);
            Long count =  menuService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }


    @PatchMapping("/del")
    @ApiOperation(value = "根据id批量删除菜单")
    @PreAuthorize(name ="批量删除菜单",value = "menu:batchDelete")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        //批量删除
        try {
            menuService.batchDel(ids);
            return AjaxResult.success().setMsg("批量删除成功！");
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("批量删除失败！");
    }

    @GetMapping("/tree")
    @ApiOperation(value = "返回部门树")
    public List<Menu> getMenuTree(){
        return menuService.menuTree();
    }
}
